
public class Sphere extends Geometry {
	public Vector3 center;
	public double radius;
	public double sqrRadius;
	
	public Sphere()
	{
		center = new Vector3();
		radius = sqrRadius = 0.0;
	}
	
	public Sphere(Vector3 c, double r)
	{
		center = c;
		radius = r;
		sqrRadius = radius * radius;
	}
	
	public IntersectResult intersect(Ray3 ray)
	{
		Vector3 v = ray.origin.sub(center);
		double a0 = v.sqrLength() - sqrRadius;
		double dDotV = ray.direction.dot(v);
		
		if(dDotV <= 0.0)
		{
			double discr = dDotV * dDotV - a0;
			if(discr >= 0.0)
			{
				IntersectResult result = new IntersectResult();
				result.geometry = this;
				result.distance = -dDotV - Math.sqrt(discr);
				result.position = ray.getPoint(result.distance);
				result.normal = result.position.sub(center).normalize();
				
				return result;
			}
		}
		
		return new IntersectResult();
	}
}
